var bigTypeContent ={
	editIndex:	undefined,
	clickMaterialRowData : undefined,
	clickMaterialRowDataIndex : undefined,
	clickMaterialRowDataField : undefined,
	rowContextMenuIndex : undefined,
	init:function(){
		console.info("bigTypeContent.init");
		//$('#panel_bigTypeContent').layout();
		//grid
		//获取上级地址的order_no和apply_id getMyUrlQueryString
		var order_no=this.getMyUrlQueryString(window.parent.location.href,"order_no");
		var apply_id=this.getMyUrlQueryString(window.parent.location.href,"apply_id");
		console.info(window.parent.myformUtils);
		
		$.ajax({
            type: 'POST',
            url: "/weixin/printTemplate/getCommonOrderTemplates",
            data: {"type":"orderCreate","name":"制单模板"},
            success: function(data){
            	var orderTemplate = {"material_type":{"width":'100',"align":"center","hidden":false},"material_name":{"width":100,"align":"center","hidden":false},"material_model":{"width":100,"align":"center","hidden":false},"color":{"width":100,"align":"center","hidden":false},"color_description":{"width":100,"align":"center","hidden":false},"provider_name":{"width":100,"align":"center","hidden":false},"quality_type":{"width":100,"align":"center","hidden":false},"feed_total":{"width":100,"align":"center","hidden":false},"remark":{"width":100,"align":"center","hidden":false}};
            	if(data.data.length>0){
            		orderTemplate = JSON.parse(data.data[0].template);
            	}
            	console.info(orderTemplate);
            	console.info(orderTemplate["material_type"]["align"]);
				$('#grid_bigTypeContent').datagrid({
				    //url:'/weixin/createOrder/getOrderInfoTailForClassification?order_no='+order_no+'&apply_id='+apply_id,
				    data : (window.parent.myformUtils)?window.parent.myformUtils.data_formFillByAllNames:{},
					title:'',
				    fit:'false',
				    height:'100%',
				    onClickCell: bigTypeContent.onClickCell,
				    singleSelect:true,
				    columns:[[
				    	{field:'material_code',title:'物料代码',align:'center',width:100,editor:'textbox',hidden:true},
				    	{field:'material_factory',title:'仓位',align:'center',width:100,editor:'textbox',hidden:true},
						{field:'material_type',title:'物料类型',align:orderTemplate["material_type"]["align"],width:orderTemplate["material_type"]["width"],hidden:orderTemplate["material_type"]["hidden"],editor:{
							type:'combobox',
							options:{
								valueField:'materialType',
								textField:'materialType',
								method:'get',
								url:'/weixin/companyMaterialType/getCompanyMaterialTypeListByPage?status=1',
								required:true,
								mode:'remote',
								editable:false,
								loadFilter: function(data){
									var rows = [];
									for(var i= 0;i<data.returnData.length;i++){
										if(data.returnData[i].defaultWarehouse==3){
											continue;
										}
										if(window.parent.leftnavEvent.findAllMaterialType().indexOf(data.returnData[i]["defaultWarehouse"]+"|"+data.returnData[i]["materialType"]+"&")<0){
											rows.push(data.returnData[i]);
										}
									}
									return rows;
								},
								formatter:function(row){
									var defaultWarehouseStr = row.defaultWarehouse;
									if(defaultWarehouseStr==1)defaultWarehouseStr="原材料仓";
									if(defaultWarehouseStr==2)defaultWarehouseStr="辅料仓";
									if(defaultWarehouseStr==3)defaultWarehouseStr="信息备注仓";
									var s = '<span style="font-weight:bold">' + row.materialType + '</span><br/>' +
									'<span style="color:#888">' + defaultWarehouseStr + '</span>';
									return s;
								},
								onSelect: function(rec){
						            console.info(rec);
						            //更新仓位
						        	console.info(bigTypeContent.editIndex);
						            var currentRow = $('#grid_bigTypeContent').datagrid('getData').rows[bigTypeContent.editIndex];
						            currentRow.material_factory = rec.defaultWarehouse;
					            	console.info("updateRow" + bigTypeContent.editIndex);
					            	$('#grid_bigTypeContent').datagrid('updateRow',{
										index: bigTypeContent.editIndex,
										row: currentRow
									});
						        }
							}
						}},
						{field:'material_name',title:'物料名称',align:orderTemplate["material_name"]["align"],width:orderTemplate["material_name"]["width"],hidden:orderTemplate["material_name"]["hidden"]},
						{field:'material_model',title:'规格',align:orderTemplate["material_model"]["align"],width:orderTemplate["material_model"]["width"],hidden:orderTemplate["material_model"]["hidden"]},
						{field:'color',title:'色号',align:orderTemplate["color"]["align"],width:orderTemplate["color"]["width"],hidden:orderTemplate["color"]["hidden"]},
						{field:'color_description',title:'颜色描述',align:orderTemplate["color_description"]["align"],width:orderTemplate["color_description"]["width"],hidden:orderTemplate["color_description"]["hidden"]},
						{field:'provider_name',title:'供应商',align:orderTemplate["provider_name"]["align"],width:orderTemplate["provider_name"]["width"],hidden:orderTemplate["provider_name"]["hidden"],editor:'textbox'},
						{field:'quality_type',title:'材质',align:orderTemplate["quality_type"]["align"],width:orderTemplate["quality_type"]["width"],hidden:orderTemplate["quality_type"]["hidden"],editor:'textbox'},
						{field:'feed_total',title:'投料数',align:orderTemplate["feed_total"]["align"],width:orderTemplate["feed_total"]["width"],editor:'numberbox'},
						{field:'remark',title:'说明',align:orderTemplate["remark"]["align"],width:orderTemplate["remark"]["width"],hidden:orderTemplate["remark"]["hidden"],editor:'textbox'}
				    ]],
				    loadFilter: function(data){
				    	console.info("loadFilter");
				    	if(data.orderInfoTail!=undefined){
				    		var id = $(window.frameElement).parents(".draggable").find("[id^='easyuipanel_']").attr("id");
					    	//util.js
					    	var title = window.parent.getEasyuiPanel(id).title;
				    		var orderInfoTail = data.orderInfoTail;
				    		var rows=[];
				    		var index = 0 ;
				    		for(var i=0;i<orderInfoTail.length;i++){
				    			if(orderInfoTail[i]["order_s_input_val_classification"]==title){//相同分类名称的
				    				var row = {
						    				'material_code':	orderInfoTail[i]["order_s_input_material_code"],
						    				'material_factory':orderInfoTail[i]["order_s_input_material_house"],
						    				'material_type':orderInfoTail[i]["order_s_input_material_type"],
						    				'material_name':orderInfoTail[i]["order_s_input_val_material_name"],
						    				'material_model':orderInfoTail[i]["order_s_input_val_material_type"],
						    				'color':orderInfoTail[i]["order_s_input_val_color"],
						    				'color_description':orderInfoTail[i]["order_s_input_val_color_description"],
						    				'provider_name':orderInfoTail[i]["order_s_input_val_provider_name"],
						    				'quality_type':orderInfoTail[i]["order_s_input_val_quality_type"],
						    				'feed_total':orderInfoTail[i]["order_s_input_val_feed_total"],
						    				'remark':orderInfoTail[i]["order_s_input_val_remark"]
						    			} 
						    			rows[index]=row;
				    					index++;
				    			}
				    		}
				    		console.info(rows);
				    		return {"rows":rows,'total':rows.length};
				    	}else{
				    		return {"rows":[{},{},{}],total:3};
				    	}
						
					},
					onBeforeLoad:function(){
			            window.parent.$.messager.progress({title:'请等待',msg:'<span style="color:green">分类仓数据填充中...</span>'});
			            setTimeout(function(){
			            	window.parent.$.messager.progress('close');
			            },500)
					},
				    onLoadSuccess:function(){
				    	/*$('#grid_bigTypeContent').datagrid('updateRow',{
							index: 0,
							row: {
								remark: "66"
							}
						});
						$('#grid_bigTypeContent').datagrid('updateRow',{
							index: 0,
							row: {
								feed_total: "hihi"
							}
						});*/
				    },
				    onRowContextMenu:function(	e, rowIndex, rowData){
				    	e.preventDefault();//阻止向上冒泡
				    	bigTypeContent.rowContextMenuIndex = rowIndex;
				        $('#mm').menu('show',{
				            left : e.pageX,
				            top : e.pageY
				        });
				        if(rowIndex==-1){
				        	$('#mm').menu('disableItem', $('#delmaterial')[0]);
				        }else{
				        	$('#mm').menu('enableItem', $('#delmaterial')[0]);
				        }
				    }
				});
            }
		});
		
		
		//datagrid可编辑
		$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});		
		//绑定鼠标移入移出监听
		$(window.frameElement).parents(".draggable").hover(function(){
			console.info("hover");
		},function(){//取消hover
			console.info("un hover");
			if (bigTypeContent.endEditing()){
				$('#grid_bigTypeContent').datagrid('acceptChanges');
			}
		});
	},
	//编辑完成
	endEditing:function(){
		if (bigTypeContent.editIndex == undefined){return true}
		if ($('#grid_bigTypeContent').datagrid('validateRow', bigTypeContent.editIndex)){
			$('#grid_bigTypeContent').datagrid('endEdit', bigTypeContent.editIndex);
			console.info("校验编辑数据");
			//校验物料类型是否选择的
			/*if($('#grid_bigTypeContent').datagrid('getData').rows[bigTypeContent.editIndex]!=undefined){
				var check_material_factory = $('#grid_bigTypeContent').datagrid('getData').rows[bigTypeContent.editIndex]["material_factory"];
				console.info(check_material_factory);
			}*/
			bigTypeContent.editIndex = undefined;
			return true;
		} else {
			return false;
		}
	},
	//点击单元格的时候
	onClickCell:function(index, field){
		if (bigTypeContent.endEditing()){
			$('#grid_bigTypeContent').datagrid('selectRow', index)
					.datagrid('editCell', {index:index,field:field});
			bigTypeContent.editIndex = index;
		}
		//如果是 物料名称 规格 色号 颜色描述 则弹出dialog选择物料
		bigTypeContent.clickMaterialRowData = $('#grid_bigTypeContent').datagrid('getData').rows[index];
		bigTypeContent.clickMaterialRowDataIndex = index;
		bigTypeContent.clickMaterialRowDataField = field;
		console.info(bigTypeContent.clickMaterialRowData);
		if("material_name material_model color color_description".indexOf(field)>-1){
			if(bigTypeContent.clickMaterialRowData.material_type==null||bigTypeContent.clickMaterialRowData.material_factory==null){
				alert("请选择物料类型");
				return;
			}
			$('div#easyui_dialog_source_input').dialog({
	            modal: true,
	            title: '请选择',
	            width: "90%",
	            height: "90%",
	            closed: false,
	            border:false,
	            cache: false,
	            resizable: true,
	            content:"<iframe frameborder='0' scrolling='no' style='width:99%;height:97%;' src='source_factory_input_for_flc_dialog.html'></iframe>",
	            onClose : function() {  
	            	bigTypeContent.destroydialog();
	            }
	        });
		}
	},
	updateRowData:function(data){
		console.info(data);
		console.info(bigTypeContent.clickMaterialRowDataIndex);
		$('#grid_bigTypeContent').datagrid('updateRow',{
			index: bigTypeContent.clickMaterialRowDataIndex,
			row: {data:data}
		});
		bigTypeContent.endEditing();
		
	},
	destroydialog:function(){
        $("div#easyui_dialog_source_input").dialog('destroy');  
        $("body").append("<div id='easyui_dialog_source_input'></div>");
    },
    menuHandler:function(item){
    	console.info(item);
    	console.info(bigTypeContent.rowContextMenuIndex)
    	bigTypeContent.endEditing();
    	if(item.text=="添加物料"){
    		//alert("添加物料");
    		$('#grid_bigTypeContent').datagrid('appendRow',{});
    	}
    	if(item.text=="删除物料"){
    		//alert("删除物料");
    		$('#grid_bigTypeContent').datagrid('deleteRow',bigTypeContent.rowContextMenuIndex);
    	}
    },
    getRowData:function(){
    	var data = $('#grid_bigTypeContent').datagrid('getData').rows;
    	var id = $(window.frameElement).parents(".draggable").find("[id^='easyuipanel_']").attr("id");
    	//util.js
    	var title = window.parent.getEasyuiPanel(id).title;
    	for(var i=0;i<data.length;i++){
    		data[i]["classification"]=title;
    	}
    	console.info(data);
    	return data;
    },
    getMyUrlQueryString:function(url,name) { 
        var reg = new RegExp("[^\?&]?" + encodeURI(name) + "=[^&]+");
        var arr = url.match(reg);
        if (arr != null) {
            return decodeURI(arr[0].substring(arr[0].search("=") + 1));
        }
        return "";
    },
    parentAppendRow:function(row){
    	$('#grid_bigTypeContent').datagrid('appendRow',row);
    }
}

/*系统禁止右键菜单*/
function stop() {
    return false;
}
document.oncontextmenu = stop;

/*键盘按键监听*/
$(document).keyup(function(event){
	console.info(event.keyCode);
	if(event.keyCode==27){
		window.parent.basic_index.lockTemp(false);
		window.parent.basic_index.lockTemp(true);
	}
});
